package shujia.day08;

/*
    String类中的替换功能
        String replace(char old,char new)
        String replace(String old,String new)
    去除字符串两空格
        String trim()
    按字典顺序比较两个字符串【重要！！】
        int compareTo(String str) 按照字符串中的字符从左向右依次比较=，如果长度和内容都一样，返回0
        int compareToIgnoreCase(String str)


 */
public class StringDemo6 {
    public static void main(String[] args) {
        String s1 = "今天是20205年1月4日，hello今年是蛇年hellodsaqwwdq";
        //String replace(char old,char new) 使用新字符替换所有的旧字符
        String s2 = s1.replace('h', '_');
        System.out.println("s1: "+s1);
        System.out.println("s2: "+s2);

        System.out.println("--------------------");
        //String replace(String old,String new) 使用新的字符串替换旧字符串
        String s3 = s1.replace("hello", "flinkspark");
        System.out.println("s1: "+s1);
        System.out.println("s3: "+s3);

        System.out.println("--------------------");
        String s4 = "    hello world    ";
        System.out.println(s4.trim());
        System.out.println("--------------------");

        String s5 = "hello";
        String s6 = "world";
        String s7 = "hel";
        System.out.println(s5.compareTo(s6)); // -15
        System.out.println(s5.compareTo(s7)); // 2

        // "hello","apple","world","flink","hadoop"


    }
}


/*
    String类中的compareTo源码解释：
            //s5.compareTo(s6)
            public int compareTo(String anotherString) {
                int len1 = value.length; // 5
                int len2 = anotherString.value.length; // 3
                int lim = Math.min(len1, len2); // 3
                char[] v1 = value; // ['h','e','l','l','o']
                char[] v2 = anotherString.value; // ['h','e','l']
                int k = 0;

                while (k < lim) {
                    char c1 = v1[k]; // 'h'  'e'  'l'
                    char c2 = v2[k]; // 'h'  'e'  'l'
                    if (c1 != c2) {
                        return c1 - c2; // 104 - 119 = -15
                    }
                    k++;
                }
                return len1 - len2; // 2
            }
 */